/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_volume_fog.h
 * Desc:    Water volume fog shader
 * Version: 1.11
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	#ifdef MULTISAMPLE_0
		half depth = getDeferredDepth(texture(s_texture_12,gl_FragCoord.xy * Viewport.zw));
	#else
		int2 itexcoord = int2(gl_FragCoord.xy);
		half depth = getDeferredDepth(texelFetch(s_texture_12,itexcoord,0));
		for(int i = 1; i < MULTISAMPLE_SAMPLES; i++) {
			depth = max(depth,getDeferredDepth(texelFetch(s_texture_12,itexcoord,i)));
		}
	#endif
	
	half distance = length(s_texcoord_1.xyz);
	half fog_depth = max(depth - distance,0.0f);
	
	half fog = exp2(-pow(half(s_texcoord_1.w) * fog_depth,volume_power));
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	#ifdef MULTISAMPLE_0
		half depth = getDeferredDepth(s_texture_12.SampleLevel(s_sampler_12,IN.position.xy * Viewport.zw,0.0f));
	#else
		int2 itexcoord = int2(IN.position.xy);
		half depth = getDeferredDepth(s_texture_12.Load(itexcoord,0));
		[unroll] for(int i = 1; i < MULTISAMPLE_SAMPLES; i++) {
			depth = max(depth,getDeferredDepth(s_texture_12.Load(itexcoord,i)));
		}
	#endif
	
	half distance = length(IN.texcoord_1.xyz);
	half fog_depth = max(depth - distance,0.0f);
	
	half fog = exp2(-pow(IN.texcoord_1.w * fog_depth,volume_power));
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	half depth = getDeferredDepth(tex2Dproj(s_texture_12,IN.texcoord_7));
	
	half distance = length(IN.texcoord_1.xyz);
	half fog_depth = max(depth - distance,0.0f);
	
	half fog = exp2(-pow(half(IN.texcoord_1.w) * fog_depth,volume_power));
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	half depth = getDeferredDepth(h4tex2Dproj(s_texture_12,IN.texcoord_9.xyw));
	
	half distance = length(IN.texcoord_1.xyz);
	half fog_depth = max(depth - distance,0.0f);
	
	half fog = exp2(-pow(half(IN.texcoord_1.w) * fog_depth,volume_power));
	
#endif
